package com.flyone.qybackend.controller;

import com.flyone.qybackend.common.BaseResponse;
import com.flyone.qybackend.common.ErrorCode;
import com.flyone.qybackend.common.ResultUtils;
import com.flyone.qybackend.exception.BusinessException;
import com.flyone.qybackend.model.dto.diary.DiarySaveRequest;
import com.flyone.qybackend.model.entity.Diary;
import com.flyone.qybackend.model.entity.ScoreDiary;
import com.flyone.qybackend.model.entity.User;
import com.flyone.qybackend.service.DiaryService;
import com.flyone.qybackend.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/diary")
@Slf4j
public class DiaryController {

    @Resource
    private UserService userService;

    @Resource
    private DiaryService diaryService;

    @Resource
    private StringRedisTemplate stringRedisTemplate;


    /**
     * 获取当日日记内容
     * @param request
     * @return
     */
    @GetMapping("/list")
    public BaseResponse<List<Diary>> getDiaryWithDay(@RequestParam String date,HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        Long userId = loginUser.getId();

        List<Diary> diaryList = diaryService.getDiaryWithDay(date, userId);
        return ResultUtils.success(diaryList);
    }

    /**
     * 保存日记内容
     * @param diarySaveRequest
     * @param request
     * @return
     */
    @PostMapping("/save")
    public BaseResponse<Long> saveDiary(@RequestBody DiarySaveRequest diarySaveRequest, HttpServletRequest request){
        if (diarySaveRequest == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        Long userId = loginUser.getId();

        long diaryId = diaryService.saveWrite(diarySaveRequest, userId);

        return ResultUtils.success(diaryId);
    }
    /**
     * 修改日记内容
     * @param diarySaveRequest
     * @param request
     * @return
     */
    @PostMapping("/update")
    public BaseResponse<Boolean> updateDiary(@RequestBody DiarySaveRequest diarySaveRequest, HttpServletRequest request){
        if (diarySaveRequest == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        Long userId = loginUser.getId();

        boolean result = diaryService.updateWrite(diarySaveRequest, userId);

        return ResultUtils.success(result);
    }
    /**
     * 删除日记
     */
    @GetMapping("/remove")
    public BaseResponse<Boolean> removeDiary(@RequestParam("id") long diaryId, HttpServletRequest request){
        User loginUser = userService.getLoginUser(request);
        Long userId = loginUser.getId();

        boolean result = diaryService.removeDiary(diaryId, userId);

        return ResultUtils.success(result);
    }

    /**
     * 获取心情指数及日记情绪
     * @param mouth
     * @param request
     * @return
     */
    @GetMapping("/scores")
    public BaseResponse<ScoreDiary> getScoreWithDiary(int mouth, HttpServletRequest request){
        if (mouth < 1 || mouth > 12){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        Long userId = loginUser.getId();
        ScoreDiary scoreDiary = diaryService.getScoreWithDiary(mouth, userId);

        return ResultUtils.success(scoreDiary);
    }

    /**
     * 获取 bilibili 嵌套代码
     * @return
     */
    @GetMapping("/videos")
    public BaseResponse<List<String>> getBiliVideoList(@RequestParam(value = "page") int page) {
        String iframes = stringRedisTemplate.opsForValue().get("qy:iframes:" + page);
        String replace = null;
        if (iframes != null) {
            replace = iframes.replace("[", "")
                    .replace("]", "")
                    .replaceAll("'", "");
        }
        String[] split = new String[0];
        if (replace != null) {
            split = replace.split(",");
        }
        List<String> iframeList = new ArrayList<>(Arrays.asList(split));
        return ResultUtils.success(iframeList);
    }
}
